/*
 * Femto OS v 0.92 - Copyright (C) 2008-2010 Ruud Vlaming
 *
 * This file is part of the Femto OS distribution.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, version 3 of the License.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * Please note that, due to the GPLv3 license, for application of this
 * work and/or combined work in embedded systems special obligations apply.
 * If these are not to you liking, please know the Femto OS is dual
 * licensed. A commercial license and support are available.
 * See http://www.femtoos.org/ for details.
 */

/**
 * This file is solely for demonstration purposes.
 *
 * The Hello World example is made to get you started and this is the file
 * you want to play with. Do not use other examples to that end, that will
 * be a disappointing experience (or a steep learning curve)
 *
 * Explanation on the website, point your browser at:
 * http://www.femtoos.org/code_start.html
 */

/* This this the only include needed in your code .*/
#include "femtoos_code.h"

void USART_Transmit( unsigned char);
void USART_gets(char *);
void fIm_printf(char *);
unsigned char USART_Receive();

//---------------------------
//  Initialization Procedure
//---------------------------
void appBoot(void)
{   
    devLedDRR    = 0xFF;
    devLedPORT   = 0xFF;
    devSwitchDRR = 0x00; 
    DDRA=0xFF;
	//DDRD=0xFF;
	DDRC=0xFF;

    // USART initialization
    // Communication Parameters: 8 Data, 1 Stop, No Parity
    // USART Receiver: On
    // USART Transmitter: On
    // USART Mode: Asynchronous
    // USART Baud Rate: 115200 (Double Speed Mode)
    UCSRA=0x02;
    UCSRB=0x18; 
    UCSRC=0x86;
    UBRRH=0x00;
    UBRRL=0x08;
}

//-------------------
//  Task 1 Procedure
//-------------------
#if (preTaskDefined(taskIm1))
void appLoop_taskIm1(void)
{ 
    while(true)
	{
	    PORTA=0xFF;
	    taskDelayFromNow(2000);
		PORTA=0x00;
		taskDelayFromNow(2000);
	}
}
#endif

//-------------------
//  Task 2 Procedure
//-------------------
#if (preTaskDefined(taskIm2))
void appLoop_taskIm2(void)
{ 
    while(true)
	{
	    //PORTD=0xFF;
	    taskDelayFromNow(1000);
		//PORTD=0x00;
		taskDelayFromNow(1000);
	}    
}
#endif

//-------------------
//  Task 3 Procedure
//-------------------
#if (preTaskDefined(taskIm3))
void appLoop_taskIm3(void)
{ 
    while(true)
	{
	    PORTC=0xFF;
	    taskDelayFromNow(1000);
		PORTC=0x00;
		taskDelayFromNow(1000);
	}    
}
#endif

//-------------------
//  Task 4 Procedure
//-------------------
#if (preTaskDefined(taskIm4))
void appLoop_taskIm4(void)
{ 
	char sInputCmd[20];
    fIm_printf("\r\nWelcome to FemtoOS command line tool Ver. ");
	fIm_printf(MAIN_VER);
	fIm_printf("\r\n");
    fIm_printf("This version modified by Imam Sumarsono\r\n");
    fIm_printf("============================================\r\n");
    while(true)
	{
	    fIm_printf("CLI> ");
		USART_gets(sInputCmd);
		fIm_printf("\r\nInput command : ");
		fIm_printf(sInputCmd);
	    fIm_printf("\r\n");
	}    
}
#endif


void fIm_printf(char *sdata)
{
    char c=0;
    while(*(sdata+c)!=0)
	{ 
	    USART_Transmit(*(sdata+c));
		c++;
	}
}
void USART_Transmit( unsigned char data )
{
	/* Wait for empty transmit buffer */
	while ( !( UCSRA & (1<<UDRE)) )
	{}
	/* Put data into buffer, sends the data */
	UDR = data;
}
unsigned char USART_Receive()
{
    unsigned char cInput;
    /* Wait for data to be received */
    while ( !(UCSRA & (1<<RXC)) )
	{
	    taskDelayFromNow(1);
	}
    /* Get and return received data from buffer */
	cInput = UDR;
	USART_Transmit(cInput);
    return cInput;
}
void USART_gets(char *stringGet)
{
	unsigned char cdata;
	unsigned int  counter=0;
	cdata = USART_Receive();
	while( cdata != 0x0d)
	{
		*(stringGet+counter++)=cdata;
		cdata = USART_Receive();
	}
	*(stringGet+counter)=0;
}

